package com.czy.exce;



/**
 * @author chenzy
 * @since 2021-01-15
 */
public class MyException extends RuntimeException {
    public final static int ALLException=-66;
    public final static int Info=-45;
    private final Integer code;
    private Object data;

    private MyException() {
        this(ALLException,"系统异常");
    }


    private MyException(String message) {
        this(ALLException, message);
    }

    private MyException(Integer code, String message) {
        this(code, message, null);
    }

    private MyException(Integer code, String message, boolean fillInStackTrace) {
        super(fillInStackTrace ? null : message, fillInStackTrace ? new RuntimeException(message) : null);
        this.code = code;
    }

    private MyException(Integer code, String message, Throwable e) {
        super(message, e);
        this.code = code;
    }


    public static <T> MyException error(Integer code, String msg, T data) {
        return new MyException(code, msg).setData(data);
    }

    public static MyException error(Integer code, String message) {
        return new MyException(code, message);
    }

    public static MyException error(Integer code, String message, boolean fillInStackTrace) {
        return new MyException(code, message, fillInStackTrace);
    }


    public static MyException error(Integer code, String message, Throwable throwable) {
        return new MyException(code, message, throwable);
    }

    public static MyException info(String msg) {
        return error(Info, msg);
    }

    public static MyException error(String msg) {
        return error(ALLException, msg);
    }

    public Object getData() {
        return data;
    }

    public MyException setData(Object data) {
        this.data = data;
        return this;
    }

    @Override
    public String getMessage() {
        return super.getMessage();
    }

    /**
     * 重写堆栈填充，大多数情况不需要填充错误堆栈信息，提高性能
     */
    @Override
    public MyException fillInStackTrace() {
        return this;
    }

    public Integer getCode() {
        return code;
    }
}

